<?php
namespace Admin\Model;

use Think\Model;

class UserModel extends Model{
    protected $patchValidate = true;
    protected $_validate = array(
        array('username','require','用户名不能为空！',self::MUST_VALIDATE,'','add'),
        array('username','checkAdmin_add','用户名已经被使用！',self::MUST_VALIDATE,'callback','add'),
        array('username','checkAdmin','用户名已经被使用！',self::MUST_VALIDATE,'callback','edit'),
        array('password','require','密码不能为空！',self::MUST_VALIDATE,'','add'),
        array('password','/^[a-zA-Z0-9_*@#]{6,18}$/','密码必须由6-18位字母、数字或特殊字符( _ , * , # , @ )组成！',self::MUST_VALIDATE,'regex','add'),
        array('repassword','password','确认密码错误！',self::MUST_VALIDATE,'confirm','add'),
        array('tel','require','联系方式不能为空！',self::MUST_VALIDATE),
        array('tel','checkTel','联系方式不合法！',self::MUST_VALIDATE,'callback'),
        array('tel','checkTelUnique_add','联系方式已经被使用！',self::MUST_VALIDATE,'callback','add'),
        array('tel','checkTelUnique','联系方式已经被使用！',self::MUST_VALIDATE,'callback','edit'),
        array('eName','require','昵称不能为空！',self::MUST_VALIDATE),
    );
    protected function checkTel($param){
        if(!preg_match( '/^1[3-9]\d{9}$/', $param ) && !preg_match( '/^0([12]\d{9}|[3-9]\d{9,10})$/', $param )){
            return false;
        }
    }
    protected function checkAdmin_add($param){
        $where="username='%s'";
        $count=M("Admin")->where($where,$param)->count();
        if($count){
            return false;
        }
    }

    protected function checkAdmin($param){
        $where="username='%s' AND id !='%d'";
        $count=M("Admin")->where($where,$param,$this->id)->count();
        if($count){
            return false;
        }
    }
    protected function checkTelUnique_add($param){
        $where="tel='%s'";
        $count=M("Admin")->where($where,$param)->count();
        if($count){
            return false;
        }
    }

    protected function checkTelUnique($param){
        $where="tel='%s' AND id !='%d'";
        $count=M("Admin")->where($where,$param,$this->id)->count();
        if($count){
            return false;
        }
    }
}

?>